﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLib.Accounts.Bus;

namespace Ewts.Cumtomer
{
    public partial class CumAccounAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataLib.Model.Cumtomer model = new DataLib.BLL.Cumtomer().GetModel(int.Parse(Request.Params["cumid"].ToString()));
                txtCompany.Text = model.companyname;
                txtUserName.Text = new DataLib.BLL.cum_aacounts().GetCumUserName(model.customercode);
                hidcumid.Value = model.id.ToString();

                Role role = new Role();
                chkLoadSearch.Value = role.GetRole("Load查询").RoleID.ToString();
                chkGoodsStatus.Value = role.GetRole("货物状态").RoleID.ToString();
                chkGoodsItems.Value = role.GetRole("原料明细").RoleID.ToString();
            } 
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = CheckInput();

            DataLib.BLL.cum_aacounts cum = new DataLib.BLL.cum_aacounts();
            


            if (strErr == "")
            {
                User newuser = new User();
                newuser.UserName = txtUserName.Text;
                newuser.TrueName = txtTruename.Text;
                newuser.Password = AccountsPrincipal.EncryptPassword(txtPwd.Text);
                newuser.Phone = this.txtphone.Text.Trim();
                newuser.Email = txtemail.Text;
                newuser.Department = "客户";
                newuser.UserType = "CC";
                newuser.CompanyCity = newuser.CompanyCity;
                //newuser.UserType = dropCategory.SelectedValue;
                int useid = newuser.Create();
                if (chkGoodsItems.Checked)
                {
                    newuser.AddToRole(int.Parse(chkGoodsItems.Value));
                }
                if (chkGoodsStatus.Checked)
                {
                    newuser.AddToRole(int.Parse(chkGoodsStatus.Value));
                }
                if (chkLoadSearch.Checked)
                {
                    newuser.AddToRole(int.Parse(chkLoadSearch.Value));
                }
                if (useid != -100)
                {
                    DataLib.Model.cum_aacounts cumacc = new DataLib.Model.cum_aacounts();
                    cumacc.cumid = int.Parse(hidcumid.Value);
                    cumacc.accountid = useid;
                    if (new DataLib.BLL.cum_aacounts().Add(cumacc))
                    {
                        DataLib.Common.MessageBox.Show(this, "新增成功！");
                        ClearText();
                    }

                 }
            }
            else
            {
                DataLib.Common.MessageBox.Show(this, strErr);
                return;
            }
        }
        protected string CheckInput()
        {

            string strErr = "";
            if (txtUserName.Text == "" || txtUserName.Text == null) strErr += "账户名不能为空！\r\n";
            if (txtPwd.Text == "" || txtPwd.Text == null) strErr += "密码不能为空！\r\n";
            if (txtTruename.Text == "" || txtTruename.Text == null) strErr += "真实姓名不能为空！\r\n";


            if (new User().HasUser(txtUserName.Text))
            {
                strErr += "该用户名已存在！";
            }
            return strErr;
        }

        protected void ClearText()
        {
            txtUserName.Text = "";
            txtPwd.Text = "";
            txtTruename.Text = "";
            txtphone.Text = "";
            txtemail.Text = "";
        }
    }
}